<template>
	<view>
		<!-- 使用自定义的搜索组件 -->
		<my-search @click.native="gotoSearch"></my-search>
		<view class="scroll-view-container">
			<!-- 左侧的滚动视图 -->
			<scroll-view scroll-y="true" class="left-scroll-view" :style="{'height': wh + 'px'}">
				<view class="left-scroll-view-item" :class="{active: currentIndex === index}"
					@click="changeCurrentIndex(index)" v-for="(item,index) in cateList" :key="index">
					{{item.cat_name}}
				</view>


			</scroll-view>
			<!-- 右侧的滚动视图 -->
			<scroll-view scroll-y="true" class="right-scroll-view" :style="{'height': wh + 'px'}"
				:scroll-top="scrollTop">
				<view v-for="(item,index) in cateLevel2" :key="index" class="cate-lv2">
					<view class="cate-lv2-title">/ {{item.cat_name}} /</view>
					<view class="cate-lv3-list">
						<view class="cate-lv3-item" v-for="(item1,index1) in item.children" :key="index1"
							@click="gotoGoodsList(item1)">
							<image :src="item1.cat_icon" mode="widthFix"></image>
							<text> {{item1.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		data() {
			return {
				//滚动高度
				wh: 0,
				//分类列表数据
				cateList: [],
				//二级分类列表
				cateLevel2: [],
				// 当前选中项索引
				currentIndex: 0,
				// 滚动条距离顶部的距离
				scrollTop: 0,
			};
		},
		mixins: [badgeMix],
		onLoad() {
			// 获取设备相关信息
			const sysInfo = uni.getSystemInfoSync();
			this.wh = sysInfo.windowHeight - 50;
			// 获取分类列表数据
			this.getCateList();
		},
		methods: {
			async getCateList() {
				const {
					data: res
				} = await uni.$http.get('/categories');
				if (res.meta.status !== 200) return uni.$showMsg();
				this.cateList = res.message;
				this.cateLevel2 = res.message[0].children;
				console.log(this.cateList);
			},
			changeCurrentIndex(index) {
				this.currentIndex = index;
				// 获取选中的分类二级数据
				this.cateLevel2 = this.cateList[index].children;
				this.scrollTop = this.scrollTop === 0 ? 1 : 0;
			},
			gotoSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
				console.log(1);
			},
			gotoGoodsList(item1) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?cid=' + item1.cat_id,
				})
			}
		}
	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;

		.left-scroll-view {
			width: 120px;

			.left-scroll-view-item {
				background-color: #f7f7f7f;
				line-height: 60px;
				text-align: center;
				font-size: 24rpx;

				&.active {
					background-color: #fff;
					position: relative;

					&::before {
						content: '';
						display: block;
						width: 3px;
						height: 30px;
						background-color: #c00000;
						position: absolute;
						left: 0;
						top: 25%;

					}
				}
			}
		}

		.right-scroll-view {
			background-color: #fff;

			.cate-lv2-title {
				font-size: 24rpx;
				font-weight: 700;
				text-align: center;
				padding: 30rpx 0;
			}

			.cate-lv3-list {
				display: flex;
				flex-wrap: wrap;
				justify-content: space-around;
				text-align: center;

				.cate-lv3-item {
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;
					width: 33.3%;
					margin-bottom: 20rpx;

					image {
						width: 120rpx;
					}

					text {
						font-size: 24rpx;
					}


				}
			}
		}
	}
</style>
